package com.example.mysaat.service_menu_manual_test.allActivities;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.example.mysaat.R;
import com.example.mysaat.handle_utils.ManagerActivityInterface;
import com.example.mysaat.handle_utils.ManagerActivityUtilProxy;
import com.example.mysaat.service_menu_manual_test.AllFunctions.Battery;
import com.example.mysaat.service_menu_manual_test.AllFunctions.EarPhoneInspect;
import com.example.mysaat.service_menu_manual_test.AllFunctions.SDCard;
import com.example.mysaat.service_menu_manual_test.AllFunctions.SIM1;
import com.example.mysaat.service_menu_manual_test.AllFunctions.SIM2;
import com.example.mysaat.service_menu_manual_test.AutoPressListViewAdapter;
import com.example.mysaat.service_menu_manual_test.Listeners.MyFailOnclickListener;
import com.example.mysaat.service_menu_manual_test.Listeners.MyPassOnclickListener;

public class AutoPressActivity extends AppCompatActivity {
    private Button passBtn = null;
    private Button failBtn = null;
    private ListView listView = null;
    private SIM1 sim1 = null;
    private SIM2 sim2 = null;
    private SDCard sdCard = null;
    private EarPhoneInspect earPhoneInspect = null;
    private Battery battery =null;
    private String []names = {"USIM Card","USIM Card2","SD Card","Ear Phone wrong","TA wrong","Battery","Battery ID"};
    private int pressKeyCount = 0;
    private long pressKeyStartTime=0;
    private String []results=new String[names.length];
    private ManagerActivityUtilProxy<ManagerActivityInterface<Activity>> a = null;
    //打算采用工厂模式,策略模式和构建者模式
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_press);
        init();
        failBtn.setVisibility(View.INVISIBLE);
        passBtn.setVisibility(View.INVISIBLE);
        battery.checkPowerInfo();
    }

    private void init(){
        listView = findViewById(R.id.list_view_result);
        sim1 = new SIM1(AutoPressActivity.this,0);
        sim2 = new SIM2(AutoPressActivity.this,1);
        sdCard = new SDCard(AutoPressActivity.this);
        earPhoneInspect = new EarPhoneInspect(AutoPressActivity.this);
        battery = new Battery(AutoPressActivity.this);
        failBtn = findViewById(R.id.detect_fail_btn);
        passBtn = findViewById(R.id.detect_pass_btn);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onStart() {
        super.onStart();
        String isSimPass = sim1.testSIM()?"Pass":"Fail";
        String isSim2Pass = sim2.testSIM2()?"Pass":"Fail";
        String checkSdcard = sdCard.isExistSdCardPass()?"Pass":"Fail";
        String  isEarphonePass = earPhoneInspect.isHeadsetPass()?"Pass":"Fail";
        String checkBatteryID = battery.getBatteryID().length()>0?"Pass":"Fail";
        String batteryCapacity = battery.getBatteryCapacity();
        String isCableConnect = battery.getBatteryChange()?"Pass":"Fail";

        results[0] = isSimPass;
        results[1] = isSim2Pass;
        results[2] = checkSdcard;
        results[3] = isEarphonePass;
        results[4] = isCableConnect;
        results[5] = batteryCapacity;
        results[6] = checkBatteryID;

        listView.setAdapter(new AutoPressListViewAdapter(this,names,results));
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActivityManager manager = (ActivityManager)getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        manager.moveTaskToFront(getTaskId(),0);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(sdCard.isRegister())
        sdCard.mUnregisterReceiver();
        if(earPhoneInspect.isRegister)
        earPhoneInspect.mUnregisterBroadcast();
        if(battery.isRegister)
        battery.mUnregisterBatteryChangeBroadcast();
    }


    //通过监听自检查并更新数据
    public void updateEarphoneStatus(){
        String  isEarphonePass = earPhoneInspect.isHeadsetPass()?"Pass":"Fail";
        results[3] = isEarphonePass;
        listView.setAdapter(new AutoPressListViewAdapter(this,names,results));
    }

    public void updateCableConnectStatus(){
        String isCableConnect = battery.getBatteryChange()?"Pass":"Fail";
        results[4] = isCableConnect;
        listView.setAdapter(new AutoPressListViewAdapter(this,names,results));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch(keyCode){
            case KeyEvent.KEYCODE_VOLUME_UP:
                if(pressKeyCount==0){
                    pressKeyStartTime = System.currentTimeMillis();
                    pressKeyCount++;
                }else{
                        pressKeyCount++;
                    if((System.currentTimeMillis()-pressKeyStartTime)<500){
                        failBtn.setVisibility(View.VISIBLE);
                        passBtn.setVisibility(View.VISIBLE);
                        failBtn.setOnClickListener(new MyFailOnclickListener(AutoPressActivity.this,null));
                        passBtn.setOnClickListener(new MyPassOnclickListener(AutoPressActivity.this,null));
                    }
                        pressKeyStartTime = System.currentTimeMillis();
                }
            case KeyEvent.KEYCODE_BACK:
                return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}
